package myJs.api

import myJs.myPkg.MyFetch
import shared.Pojo._
import upickle.default.{read, write}
import myJs.Implicits._
import org.scalajs.dom.window
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import upickle.default._

/**
 * Created by yz on 7/5/2022
 */
object Api {

  val apiUrl = window.location.hostname match {
    case "localhost" => "http://localhost:9000"
    case _ => "http://47.253.10.109:9003"
  }

  def images = {
    s"${apiUrl}/assets/images"
  }

  def featurePlot = {
    s"${apiUrl}/gepLiver/tool/featurePlot"
  }

  def featurePlotDownload = {
    s"${apiUrl}/gepLiver/tool/featurePlotDownload"
  }

  def dimPlot = {
    s"${apiUrl}/gepLiver/tool/dimPlot"
  }

  def dimPlotDownload = {
    s"${apiUrl}/gepLiver/tool/dimPlotDownload"
  }

  def outImages = {
    s"${apiUrl}/gepLiver/tool/outImages"
  }

  def outImagesDownload = {
    s"${apiUrl}/gepLiver/tool/outImagesDownload"
  }

  val browseCirc = BrowseCirc

  val tool = Tool

  val browseGene = BrowseGene

  val marker = Marker

  val analysis = Analysis
  val abundanceSc = AbundanceSc

  def getOrthologs(geneName: String, species: String) = {
    val url = s"${Api.apiUrl}/gepLiver/search/getOrtholog"
    MyFetch.get(url = s"${url}?geneName=${geneName}&species=${species}",
      headers = Map("Content-Type" -> "application/json")).map { any =>
      any.toJArS.toList
    }
  }

  def getMTissueBoxplotImage(request: GetMTissueBoxplotImage.Request) = {
    val url = s"${Api.apiUrl}/gepLiver/search/getMTissueBoxplotImage"
    myPost[GetMTissueBoxplotImage.Request, GetMTissueBoxplotImage.Response](url = url, request = request)
  }

  def myPost[R, P](url: String, request: R)(implicit writer: Writer[R], reader: Reader[P]) = {
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json")).map { any =>
      read[P](any.toString)
    }
  }

  def getFileColumn(fileName: String, columnName: String) = {
    val url = s"${Api.apiUrl}/gepLiver/tool/getFileColumn"
    MyFetch.get(url = s"${url}?columnName=${columnName}&fileName=${fileName}", headers = Map("Content-Type" -> "application/json")).map { any =>
      any.toJArS.toList
    }
  }

  def getBoxplotImage(geneName: String) = {
    val url = s"${Api.apiUrl}/gepLiver/explore/getBoxplotImage"
    myGet[GetBoxplotImage.Response](url = s"${url}?geneName=${geneName}")
  }

  def myGet[P](url: String)(implicit reader: Reader[P]) = {
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json")).map { str =>
      read[P](str)
    }
  }

  def getFileData(fileName: String) = {
    val url = s"${Api.apiUrl}/gepLiver/explore/getFileData"
    myGet[GetFileData.Response](url = s"${url}?fileName=${fileName}")
  }

  def searchByGeneSymbol(formData: String) = {
    val url = s"${Api.apiUrl}/gepLiver/search/searchByGeneSymbol"
    MyFetch.postRnText(url = s"${url}", data = formData, headers = Map("Content-Type" -> "application/x-www-form-urlencoded")).map { str =>
      read[List[ujson.Obj]](str)
    }
  }

  def getSelect2Values(dataStr: String) = {
    val url = s"${Api.apiUrl}/gepLiver/browseGene/getSelect2Values"
    MyFetch.post(url = s"${url}", data = dataStr, headers = Map("Content-Type" -> "application/json")).map { any =>
      any.toJDJAn
    }
  }

  def allByServer(dataStr: String) = {
    val url = s"${Api.apiUrl}/gepLiver/browseGene/allByServer"
    MyFetch.postRnText(url = s"${url}", data = dataStr, headers = Map("Content-Type" -> "application/json")).map { str =>
      str.toObj
    }
  }

  def downloadFile(fileName: String) = {
    val url = s"${Api.apiUrl}/gepLiver/tool/downloadFile"
    MyFetch.getRnStr(url = s"${url}?fileName=${fileName}").map { str =>
      str
    }
  }

}
